package com.tz.fruit.service.OrderService.Impl;


import com.tz.fruit.dao.OrderDao.OrderDao;
import com.tz.fruit.entity.Order;
import com.tz.fruit.entity.OrderItem;
import com.tz.fruit.entity.OrderStatus;
import com.tz.fruit.entity.PageBean;
import com.tz.fruit.service.OrderService.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Collections;
import java.util.Date;
import java.util.List;

/**
 * Created by Administrator on 2017/7/19.
 */
@Service
@Transactional
public class OrderServiceImpl implements OrderService {

    @Autowired
     private OrderDao orderDao;

    @Override
    public List<Order> findAll() {
      return  orderDao.findAll();
    }

    @Override
    public void save(OrderItem orderItem) {
      orderDao.save(orderItem);
    }

    @Override
    public void deleteById(Long id) {
       orderDao.deleteById(id);
    }

    @Override
    public Long findBy(Long uid, String orderNo, Date start, Date end, OrderStatus status) {
          return orderDao.findBy(uid,orderNo,start,end,status);
    }

    @Override
    public PageBean<Order> getBy(Long uid, String orderNo, Date start, Date end, String pageNow, String pageSize, OrderStatus status) {


            PageBean<Order>pageBean=new PageBean<>();
            //根据条件查出所有的order 放入集合中
            List<Order>list=orderDao.findByConditions(uid,orderNo, start, end, pageNow, pageSize, status);
            pageBean.setList(list);
            pageBean.setPageNow(Long.valueOf(pageNow));
            pageBean.setPageSize(Long.valueOf(pageSize));
            //查询出总条数
            long totalcount=orderDao.findBy(uid,orderNo, start, end, status);
            long  pageCount=totalcount/Long.valueOf(pageSize);
            if(totalcount%(Long.valueOf(pageSize))!=0){
                pageCount+=1;
            }
            pageBean.setPageCount(pageCount);
            return pageBean;
        }

    @Override
    public PageBean<Order> sortup(Long uid, String orderNo, Date start, Date end,  String pageNow, String pageSize, OrderStatus status) {
        PageBean<Order>pageBean=new PageBean<>();
        //根据条件查出所有的order 放入集合中
        List<Order>list=orderDao.findByConditions(uid,orderNo, start, end, pageNow, pageSize, status);
        Collections.sort(list);
        pageBean.setList(list);
        pageBean.setPageNow(Long.valueOf(pageNow));
        pageBean.setPageSize(Long.valueOf(pageSize));
        //查询出总条数
        long totalcount=orderDao.findBy(uid,orderNo, start, end, status);
        long  pageCount=totalcount/Long.valueOf(pageSize);
        if(totalcount%(Long.valueOf(pageSize))!=0){
            pageCount+=1;
        }
        pageBean.setPageCount(pageCount);
        return pageBean;
    }

    @Override
    public PageBean<Order> sortdown(Long uid, String orderNo, Date start, Date end,  String pageNow, String pageSize, OrderStatus status) {
        PageBean<Order>pageBean=new PageBean<>();
        //根据条件查出所有的order 放入集合中
        List<Order>list=orderDao.findByConditions(uid,orderNo, start, end, pageNow, pageSize, status);
        Collections.sort(list);
        Collections.reverse(list);
        pageBean.setList(list);
        pageBean.setPageNow(Long.valueOf(pageNow));
        pageBean.setPageSize(Long.valueOf(pageSize));
        //查询出总条数
        long totalcount=orderDao.findBy(uid,orderNo, start, end, status);
        long  pageCount=totalcount/Long.valueOf(pageSize);
        if(totalcount%(Long.valueOf(pageSize))!=0){
            pageCount+=1;
        }
        pageBean.setPageCount(pageCount);
        return pageBean;
    }


}
